package com.qf.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsCouponUserMapper;
import com.qf.pojo.DtsCouponUser;
import com.qf.service.DtsCouponUserService;
import com.qf.vo.DtsCouponVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * projectName:bj-2107-code
 * author:zxf
 * time:2021/12/1 9:58
 * description:
 */
@Service
public class DtsCouponUserServiceImpl implements DtsCouponUserService {

    @Autowired
    private DtsCouponUserMapper dtsCouponUserMapper;

    @Override
    public IPage<DtsCouponVo> findUserPage(Integer page, Integer limit, Integer couponId, Integer userId, String status, String sort, String order) {
        IPage<DtsCouponUser> ipage = new Page<>(page, limit);

        QueryWrapper<DtsCouponUser> wrapper = new QueryWrapper<>();
        wrapper.eq("coupon_id", couponId);

        if(!StringUtils.isEmpty(userId)){
            wrapper.eq("user_id", userId);
        }

        if(!StringUtils.isEmpty(status)){
            wrapper.eq("status", status);
        }

        if(order.equalsIgnoreCase("asc")){
            wrapper.orderByAsc(sort);
        }else{
            wrapper.orderByDesc(sort);
        }
        ipage = dtsCouponUserMapper.selectPage(ipage,wrapper);
        List<DtsCouponUser> records = ipage.getRecords();
        List<DtsCouponVo> list = records.stream().map(dtsCouponUser -> {
            DtsCouponVo couponVo = new DtsCouponVo();
            couponVo.setId(dtsCouponUser.getId());
            couponVo.setCouponId(dtsCouponUser.getCouponId());
            couponVo.setUserId(dtsCouponUser.getUserId());
            couponVo.setStatus(dtsCouponUser.getStatus());
            couponVo.setAddTime(dtsCouponUser.getAddTime());
            couponVo.setEndTime(dtsCouponUser.getEndTime());
            couponVo.setStartTime(dtsCouponUser.getStartTime());
            couponVo.setUpdateTime(dtsCouponUser.getUpdateTime());
            couponVo.setDeleted(dtsCouponUser.getDeleted());
            return couponVo;
        }).collect(Collectors.toList());
        IPage<DtsCouponVo> page1 = new Page<>();
        page1.setTotal(ipage.getTotal());
        page1.setRecords(list);
        return page1;
    }

    @Override
    public List<DtsCouponUser> findByCouponId(Integer id) {
        LambdaQueryWrapper<DtsCouponUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(DtsCouponUser::getCouponId,id);
        return dtsCouponUserMapper.selectList(wrapper);
    }
}